package com.bdcom.autotest.ui.snp;

import com.bdcom.autotest.interaction.bean.DeviceUpdBean;
import com.bdcom.autotest.interaction.bean.TestLog;
import com.bdcom.autotest.interaction.db.DBUtil;
import com.bdcom.autotest.util.UIUtil;
import com.bdcom.autotest.util.excel.FUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

public class PanelForExportByWorkOrder extends AbsPanel {
	public PanelForExportByWorkOrder() {
		this.setBackground(Color.WHITE);
		this.setPreferredSize(new Dimension( 0 , 250 ));
		JPanel p1 = UIUtil.createPanel();
		Font font2 = new Font("Dialog",Font.PLAIN, 14);
		JLabel jLabel0 = new JLabel( "工单号:");
		jLabel0.setFont(font2);
		JTextField wsn = new JTextField(18);
		wsn.setName("0");
		wsn.setFont(font2);
		wsn.setText("");
		p1.add(jLabel0);
		p1.add(wsn);
		p1.add(Box.createHorizontalStrut(10) );
		JPanel p2 = UIUtil.createPanel();
		JButton btn2 = new JButton("导出");
		btn2.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    String workOrder = wsn.getText();
                List<TestLog> logList = DBUtil.queryTestLogList(workOrder);
                if(logList.size() == 0){
                    JOptionPane.showMessageDialog(null, "未查询到记录！");
                }else{
                    JFileChooser jfc=new JFileChooser();
                    jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
                    jfc.showDialog(new JLabel(), "选择");
                    File file=jfc.getSelectedFile();
                    if(file.isDirectory()){
                        exportLog(logList,file.getAbsolutePath(),workOrder);
                    }else if(file.isFile()){
                        JOptionPane.showMessageDialog(null, "请选择要导出的文件夹！");
                    }

                }

			}
		});
		p2.add(Box.createHorizontalStrut( 20 ));
		p2.add(btn2);
		this.add(p1);
		this.add(p2);
	}

	private boolean exportLog(List<TestLog> logList,String path,String workOrder){
		String dstFile = path+File.separator+workOrder+"_"+System.currentTimeMillis()+".xls";
		XSSFWorkbook wb = new XSSFWorkbook();
		Sheet rsheet1 = wb.createSheet();
		Row row = rsheet1.createRow(0);
		Cell cell1 = row.createCell(0);
		cell1.setCellValue("工单号");
		Cell cell12 = row.createCell(1);
		cell12.setCellValue("OEMSN");
		Cell cell3 = row.createCell(2);
		cell3.setCellValue("BDSN");
		Cell cell4 = row.createCell(3);
		cell4.setCellValue("MAC");
		Cell cell5 = row.createCell(4);
		cell5.setCellValue("结果");
		Cell cell6 = row.createCell(5);
		cell6.setCellValue("原因");
		Cell cell7 = row.createCell(6);
        cell7.setCellValue("时间");

		int counter = 1;
		for(TestLog pp :  logList ) {
			Row createRow = rsheet1.createRow( counter );
			FUtil.createRow(pp, createRow , wb);
			counter++ ;
		}
		FUtil.writeExcel( (XSSFWorkbook)wb, dstFile);
		JOptionPane.showMessageDialog(null, "导出成功,文件路径:"+dstFile);
		return true;
	}
}
